package com.lagou.frame;

import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.WebApplicationInitializer;

import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.HandlesTypes;
import java.lang.reflect.Modifier;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

/**
 * @ClassName MySpringServletContainerInitializer
 * @Description TODO
 * @Auth ying.xiao
 * @Date 2021/3/3
 * @Version 1.0.0
 */
@HandlesTypes(MyWebApplicationInitializer.class)
public class MySpringServletContainerInitializer implements ServletContainerInitializer {

	@Override
	public void onStartup(Set<Class<?>> set, ServletContext servletContext) throws ServletException {
		List<WebApplicationInitializer> initializers = new LinkedList();
		Iterator var4;
		if (set != null) {
			var4 = set.iterator();
			while(var4.hasNext()) {
				Class<?> waiClass = (Class)var4.next();
				if (!waiClass.isInterface() && !Modifier.isAbstract(waiClass.getModifiers()) && WebApplicationInitializer.class.isAssignableFrom(waiClass)) {
					try {
						initializers.add((WebApplicationInitializer) ReflectionUtils.accessibleConstructor(waiClass, new Class[0]).newInstance());
					} catch (Throwable var7) {
						throw new ServletException("Failed to instantiate WebApplicationInitializer class", var7);
					}
				}
			}
		}

		if (initializers.isEmpty()) {
			servletContext.log("No Spring WebApplicationInitializer types detected on classpath");
		} else {
			servletContext.log(initializers.size() + " Spring WebApplicationInitializers detected on classpath");
			AnnotationAwareOrderComparator.sort(initializers);
			var4 = initializers.iterator();

			while(var4.hasNext()) {
				WebApplicationInitializer initializer = (WebApplicationInitializer)var4.next();
				initializer.onStartup(servletContext);
			}

		}

	}
}
